package cn.lxh.yatzy;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @ClassName BoolenYatzyRule
 * @Description 判断yatzy规则
 * @Author luoxiaohu
 * @Date 2023/11/1 9:55
 * @Vsersion 1.0
 **/

public class CheckYatzyRule {


    List<Integer> list = new ArrayList<>();

    /**
     * 1.去重复，重复的数字只保留一份
     * 2.重复的数字具体是
     *根据去重后的具体数字和重复数字
     * @return
     */
    public List<Integer> getRepeatNUm(int[] a) {
        Integer[] integers1 = Arrays.stream(a).boxed().toArray(Integer[]::new);
        List<Integer> integers = Arrays.asList(integers1);
        //1.去重复的
        list = integers.stream().distinct().collect(Collectors.toList());
        Set<Integer> allItems = new HashSet<>();

        return integers.stream().filter(n -> !allItems.add(n)).sorted(Comparator.reverseOrder()).distinct().collect(Collectors.toList());
    }

    /**
     *小顺子判断
     * @return
     */

    public boolean isSmallStraight(int[] a) {
        boolean flag = true;
        for (int i = 0; i < a.length-1; i++) {
            if (a[0] !=1||a[i] + 1 != a[i + 1]){
                flag = false;
                break;
            }

        }
        return flag;
    }

    /**
     * 大顺子判断
     * @return
     */
    public boolean isLargeStraight(int[] a) {
        boolean flag = true;
        for (int i = 0; i < a.length-1; i++) {
            if (a[0] !=2 ||a[i] + 1 != a[i + 1]){
                flag = false;
                break;
            }

        }

        return flag;
    }

    /**
     * 点数规则分析计算
     * @param a
     * @return
     */
    public Integer ScoreAnalyze(int[] a) {
        int score = 0;
        CountScore countScore = new CountScore();
        List<Integer> scoreList = getRepeatNUm(a);
        if (list.size() == 1){
            score = countScore.FullHouse(a);
            System.out.println("点数全部一样-50分");
        }

        if (list.size() == 2 && scoreList.size() == 2){
            score = countScore.ThreeOfAKindAndPairs(a);
            System.out.println("三个相同加一对-总数×2");
        }
        if (list.size() == 2 && scoreList.size() == 1){
            score = countScore.FourOfAKind(a,scoreList);
            System.out.println("四个相同-四个相同数字求和");
        }
        if (list.size() == 3 && scoreList.size() == 2){
            score = countScore.TwoPair(a,scoreList);
            System.out.println("两个两个相同-两对的总和");
        }
        if (list.size() == 3 && scoreList.size() == 1){
            score = countScore.ThreeOfAKind(a,scoreList);
            System.out.println("三个相同-相同三个数字的总和");
        }
        if (list.size() == 4){
            score = countScore.Pairs(a,scoreList);
            System.out.println("两个相同-两个相同的数字总和");
        }
        if (list.size() == 5 && isSmallStraight(a)){
            score = countScore.smallStraight(a);
            System.out.println("小顺子+15");
        }
        if (list.size() == 5 && isLargeStraight(a)){
            score = countScore.LargeStraight(a);
            System.out.println("大顺子+20");
        }
        if (list.size() == 5 && !isSmallStraight(a) &&!isLargeStraight(a) ){
            score = countScore.Sum(a);
            System.out.println("没规律");
        }
        return score;
    }
}
